package practice2019.dao;

import cn.hutool.crypto.SecureUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.hutool.crypto.SecureUtil;
import org.omg.PortableInterceptor.USER_EXCEPTION;
import practice2019.domain.user;

/**
 *
 * @author Gao
 * @version 1.0
 */

public class UserDao {
    /**
     * 查询所有用户
     * @return
     */
    public List<user> selectAlluser(){
        List<user> userList = new ArrayList<user>();
        Connection conn = null;
        PreparedStatement pstmt =null;
        ResultSet rs =null;

        conn = com.practice2019.util.DbUtilHikari.getConnection();
        String sql ="SELECT userid,username,userpsw FROM users";
        try {
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();

            while(rs.next()){
                user user = new user();
                user.setUserid(rs.getInt("userid"));
                user.setUsername(rs.getString("username"));
                user.setUserpsw(rs.getString("userpsw"));
                userList.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            com.practice2019.util.DbUtilHikari.close(rs,pstmt,conn);
        }
        return userList;
    }

    /**
     *
     * @param userid
     * @param uname 用户名
     * @param upsw 密码
     * @return 登录成功返回true
     */
    public int updateUser(int userid,String uname,String upsw){
        int flag = 0;
        Connection conn =null;
        PreparedStatement pstmt =null;

        conn = com.practice2019.util.DbUtilHikari.getConnection();
        String sql ="UPDATE users SET username=?,userpsw=? WHERE";
        try {
            pstmt.setString(1,uname);
            pstmt.setString(2,SecureUtil.md5(upsw));
            pstmt.setInt(3,userid);
            flag = pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {

        }
        return flag;
    }

    public int deleteUser(int userid){
        int flag = 0;

        Connection conn =null;
        PreparedStatement pstmt=null;
        conn = com.practice2019.util.DbUtilHikari.getConnection();

        String sql = "DELETE FROM users WHERE userid=?";
        try {
            pstmt = conn.prepareStatement(sql);

            pstmt.setInt(1, userid);

            flag = pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            com.practice2019.util.DbUtilHikari.close(pstmt,conn);
        }

        return flag;

    }

    public boolean login(String uname,String upsw){
        //是否登录成功
        boolean flag =false;
//        数据库连接
        Connection conn =null;
//        执行SQL语句
        PreparedStatement pstmt =null;
        ResultSet rs =null;
//        获得数据库连接
        conn = com.practice2019.util.DbUtilHikari.getConnection();

        String sql = "SELECT * FROM users WHERE username=? AND uesrpsw?";
        try {
            pstmt = conn.prepareStatement(sql);

            pstmt.setString(1,uname);
            pstmt.setString(2,SecureUtil.md5(upsw));

            rs = pstmt.executeQuery();
            /**
             * 判断rs结果集里有没有数据
             */
            if(rs.next()){
                flag = true;
            } else {
                flag = false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            com.practice2019.util.DbUtilHikari.close(rs,pstmt,conn);
        }
        return flag;
    }
    /**
     *
     * @param uname 用户名
     * @param upsw 密码
     * @return 注册成功返回1
     */
    public int insertUser(String uname,String upsw){
        /*声明一些备用变量
        * conn代表数据库连接*/
        Connection conn;
        //预编译对象，用pstmt执行SQL语句
        PreparedStatement pstmt = null;
        //注册是否成功
        int flag = 0;

        conn = com.practice2019.util.DbUtilHikari.getConnection();

        String sql = "INSERT INTO users(uesrname,userpsw) VALUES(?,?)";
        try {
            pstmt = conn.prepareStatement(sql);

            pstmt.setString(1,uname);
            pstmt.setString(2,SecureUtil.md5(upsw));

            flag = pstmt.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            com.practice2019.util.DbUtilHikari.close(pstmt,conn);
        }
        return flag;
    }
}

